package com.wkedu.repair.controller;

import com.wkedu.repair.dto.ReachDto;
import com.wkedu.repair.dto.RepairStudentDto;
import com.wkedu.repair.dto.RepairTeacherDto;
import com.wkedu.repair.dto.TeacherDto;
import com.wkedu.repair.enums.CodeEnum;
import com.wkedu.repair.exception.LoginException;
import com.wkedu.repair.form.*;
import com.wkedu.repair.reponse.Response;
import com.wkedu.repair.service.ReachService;
import com.wkedu.repair.service.RepairTeacherService;
import com.wkedu.repair.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 港港
 * @version 1.0
 * @date 2021/9/5 3:11 下午
 */
@RequestMapping("/teacher")
@CrossOrigin
@RestController
@Slf4j
public class TeacherController {

    @Autowired
    private TeacherService teacherService;
    @Autowired
    private RepairTeacherService repairTeacherService;

    @Autowired
    private ReachService reachService;

    @PostMapping("/login")
    public Response TeacherLogin(@RequestBody TeacherLoginForm teacherLoginForm) throws LoginException {
        Response<String> response = new Response<>();
        TeacherDto teacherDto = teacherService.getTeacherForUsername(teacherLoginForm.getUsername());
        if (teacherDto!=null) {
            if (teacherLoginForm.getPassword().equals(teacherDto.getPassword())) {
                response.setCode(CodeEnum.SUCCESS.getCode());
            }
        }else {
            response.setCode(CodeEnum.ERROR.getCode());
            response.setData(null);
//            throw new LoginException("找不到老师");
        }


        return response;
    }

    @PostMapping("/sign")
    public Response TeacherSign(@RequestBody TeacherSignForm teacherSignForm){
        Response<String> response = new Response<>();

        TeacherDto teacherDto1 = teacherService.getTeacherForTeacherId(teacherSignForm.getTeacherId());
        TeacherDto teacherDto2 = teacherService.getTeacherForUsername(teacherSignForm.getUsername());

        if (teacherDto1 == null && teacherDto2 == null){
            TeacherDto teacherDto = new TeacherDto(
                    teacherSignForm.getName(), teacherSignForm.getTeacherId(),
                    teacherSignForm.getUsername(), teacherSignForm.getMajor(),
                    teacherSignForm.getCourse(),teacherSignForm.getCourse()
            );

            teacherService.addTeacher(teacherDto);

            response.setCode(CodeEnum.SUCCESS.getCode());
            response.setData("教师注册成功");

        }else {
            response.setCode(CodeEnum.ERROR.getCode());
            response.setData("已有该用户");
        }




        return response;
    }

    @PostMapping("/upload")
    public Response teacherUpload(HttpServletRequest request, @RequestParam("file") MultipartFile file,
                                  @RequestParam String repairInfo,
                                  @RequestParam String repairPath,
                                  @RequestParam String teacherId)
            throws IOException {
        Response<String> response = new Response<>();

        String fileName = file.getOriginalFilename();
        String newFileName = fileName  + "_" +  repairInfo+ "_" + repairPath+ "_" + teacherId + ".jpg";
        // 得到文件保存的位置以及新文件名
        File dest = new File(System.getProperty("user.dir")+
                "/upload_photo/teacher/"+ newFileName);
        try {
            // 上传的文件被保存了
            file.transferTo(dest);
            // 打印日志
            System.out.println("上传成功，当前上传的文件保存在 {}"+ newFileName);
            // 自定义返回的统一的 JSON 格式的数据，可以直接返回这个字符串也是可以的。
            response.setCode(CodeEnum.SUCCESS.getCode());
        } catch (IOException e) {
            log.error("文件上传出错");
            response.setCode(CodeEnum.ERROR.getCode());

        }


        response.setCode(CodeEnum.SUCCESS.getCode());


        return response;
    }

    @PostMapping("/repair")
    public Response repairStudent(@RequestBody RepairTeacherForm repairTeacherForm){
        Response<String> response = new Response<>();

        RepairTeacherDto repairTeacherDto = new RepairTeacherDto(
                repairTeacherForm.getRepairInfo(), repairTeacherForm.getRepairPath(),
                repairTeacherForm.getRepairType(), repairTeacherForm.getTeacherId()
        );

        repairTeacherService.add(repairTeacherDto);

        response.setCode(CodeEnum.SUCCESS.getCode());
        response.setData("请求插入成功");

        return response;
    }

    @GetMapping("/list_repair")
    public Response listRepairTeacher(@RequestParam String teacherId) throws LoginException {

        Response<List<RepairTeacherForm>> response = new Response<>();


        List<RepairTeacherDto> repairTeacherDtoList = repairTeacherService.listForStudentId(teacherId);

        if (!repairTeacherDtoList.isEmpty()) {
            List<RepairTeacherForm> repairTeacherFormList = repairTeacherDtoList.stream().map(
                    i -> new RepairTeacherForm(
                            i.getRepairInfo(), i.getRepairPath(), i.getRepairType(), i.getTeacherId()
                    )
            ).collect(Collectors.toList());

            response.setCode(CodeEnum.SUCCESS.getCode());
            response.setData(repairTeacherFormList);
        }else {

            response.setData(null);
            throw new LoginException("没有报修信息");

        }

        return response;
    }

    @GetMapping("/get_teacher_info")
    public Response getTeacherInfo(@RequestParam String username){
        Response<TeacherInfoForm> response = new Response<>();

        TeacherDto teacherDto = teacherService.getTeacherForUsername(username);
        if (teacherDto != null){
            TeacherInfoForm teacherInfoForm = new TeacherInfoForm(
                    teacherDto.getName(), teacherDto.getUsername(), teacherDto.getTeacherId()
            );
            response.setCode(CodeEnum.SUCCESS.getCode());
            response.setData(teacherInfoForm);
        }else{
            response.setCode(CodeEnum.ERROR.getCode());
            response.setData(null);
        }


        return response;
    }


//    @GetMapping("/list_repair")
//    public Response listRepair(@RequestParam String teacherId){
//        Response<List<RepairTeacherForm>> response = new Response<>();
//
//        List<RepairTeacherDto> repairTeacherDtoList = repairTeacherService.listForStudentId(teacherId);
//
//        if (repairTeacherDtoList != null){
//            List<RepairTeacherForm> repairTeacherFormList = repairTeacherDtoList.stream().map(i -> new RepairTeacherForm(
//                    i.getRepairInfo(), i.getRepairPath(), i.getRepairType(),
//                    i.getTeacherId()
//            )).collect(Collectors.toList());
//            response.setCode(CodeEnum.SUCCESS.getCode());
//            response.setData(repairTeacherFormList);
//        }else {
//
//            response.setCode(CodeEnum.ERROR.getCode());
//            response.setData(null);
//
//        }
//
//
//        return response;
//    }


    @GetMapping("list_reach_teacher")
    public Response listReachTeacher(String teacherName){
        Response<List<ReachDto>> response = new Response<>();

        List<ReachDto> reachDtoList = reachService.listReach(teacherName);

        if (!reachDtoList.isEmpty()) {

            response.setCode(CodeEnum.SUCCESS.getCode());
            response.setData(reachDtoList);
        }else {

            response.setCode(CodeEnum.ERROR.getCode());
            response.setData(null);
        }


        return response;
    }

}
